package org.b3log;

import org.b3log.common.AsyncImageLoader;
import org.b3log.common.AsyncImageLoaderCallback;
import org.b3log.common.DensityUtil;
import org.b3log.view.ImageDialog;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ShowActivity extends Activity {
	private AsyncImageLoader loader = AsyncImageLoader.getInstance();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setTheme(R.style.titleComplex);
		setContentView(R.layout.show);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_complex);
		TextView textTitle = (TextView) this.findViewById(R.id.textTitle);
		Intent intent = getIntent();
		JSONObject object = null;
		JSONArray pics = null;
		try {
			object = new JSONObject(intent.getStringExtra("data"));
			textTitle.setText(object.getString("title"));
			pics = object.getJSONArray("pics");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		LinearLayout linear0 = (LinearLayout) findViewById(R.id.linear0);
		LinearLayout linear1 = (LinearLayout) findViewById(R.id.linear1);
		LinearLayout linear2 = (LinearLayout) findViewById(R.id.linear2);
		// WindowManager wm = (WindowManager)
		// getSystemService(Context.WINDOW_SERVICE);
		// int screenWidth = wm.getDefaultDisplay().getWidth();
		// int imgWidth = (screenWidth - DensityUtil.dip2px(this, 20)) / 3;
		// System.out.println("screenWidth:" + screenWidth);
		// System.out.println("imgWidth:" + imgWidth);
		for (int i = 0; i < pics.length(); i++) {
			String url = null;
			try {
				url = pics.getString(i);
			} catch (JSONException e) {
				e.printStackTrace();
			}
			ImageView imageView = new ImageView(this);
			imageView.setTag(url);
			imageView.setAdjustViewBounds(true);
			imageView.setScaleType(ImageView.ScaleType.FIT_START);
			imageView.setPadding(0, DensityUtil.dip2px(this, 5), 0, 0);
			// BitmapFactory.Options options = new BitmapFactory.Options();
			// options.inJustDecodeBounds = true;
			// 获取这个图片的宽和高
			// Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
			// mThumbIds[i], options); // 此时返回bm为空
			// options.inJustDecodeBounds = false;
			// 计算缩放比
			// int be = (int) (options.outWidth / ((float) imgWidth));
			// if (be <= 0)
			// be = 1;
			// options.inSampleSize = be;
			// 重新读入图片，注意这次要把options.inJustDecodeBounds 设为 false哦
			// bitmap = BitmapFactory.decodeResource(getResources(),
			// mThumbIds[i], options);
			// int w = bitmap.getWidth();
			// int h = bitmap.getHeight();
			// System.out.println(be + "|" + w + "|" + h);
			AsyncImageLoaderCallback callbackImpl = new AsyncImageLoaderCallback(imageView);
			Bitmap cacheImage = loader.loadBitmap(url, callbackImpl);
			if (null != cacheImage) {
				imageView.setImageBitmap(cacheImage);
			}
			imageView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Bitmap bitmap = loader.loadBitmap((String) v.getTag(), new AsyncImageLoader.ImageLoaderCallback() {
						public void imageLoaded(Bitmap bitmap) {
							new ImageDialog(ShowActivity.this, bitmap).show();
						}
					});
					new ImageDialog(ShowActivity.this, bitmap).show();
				}
			});
			switch (i % 3) {
			case 0:
				linear0.addView(imageView);
				break;
			case 1:
				linear1.addView(imageView);
				break;
			case 2:
				linear2.addView(imageView);
				break;
			default:
				break;
			}
		}
	}

}